package com.app.print;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.swing.JApplet;
import javax.swing.JOptionPane;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;

public class PrintView extends JApplet {
	


	public void print(String REPORT_URL,String data,String isview,String showPrintDialog,String _printCount,String sessionid) {
		
		 URL url = null;
		 int printCount = 1;
		 boolean _isview = true;
		 boolean _showPrintDialog = false;
		try {
			
	
			
			if (_printCount != null && _printCount.trim().length() != 0) {
				printCount = Integer.parseInt(_printCount);
			}
			//
			if (isview != null && isview.trim().length() != 0) {
				_isview = "true".equalsIgnoreCase(isview);
			}
			//
			if (showPrintDialog != null && showPrintDialog.trim().length() != 0) {
				_showPrintDialog = "true".equalsIgnoreCase(showPrintDialog);
			}

			url = new URL(REPORT_URL);
	
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
			e.printStackTrace();
		}
		
		
		
		


		HttpURLConnection conn = null;
		try {
			// HttpClient hc=new HttpClient(arg0, arg1, arg2)
			conn = (HttpURLConnection) url.openConnection();
			// List<String> headers = conn.getHeaderFields().get("Set-Cookie");
			conn.setRequestProperty("Cookie", "JSESSIONID=" + sessionid);
			conn.setDoOutput(true);
			if (data != null && data.trim().length() != 0) {
				conn.getOutputStream().write(data.getBytes());
				conn.getOutputStream().close();
			}

			final JasperPrint jasperPrint = (JasperPrint) JRLoader
					.loadObject(conn.getInputStream());
			//
			if (_isview) {// 预览

				// net.sf.jasperreports.view.JRViewer v=new
				// JRViewer(jasperPrint);
				// this.add(v);
				new Thread(new Runnable() {

					@Override
					public void run() {
						try {
							JasperViewer.viewReport(jasperPrint, true);
						} catch (Exception e) {
							StringWriter swriter = new StringWriter();
							PrintWriter pwriter = new PrintWriter(swriter);
							e.printStackTrace(pwriter);
							JOptionPane.showMessageDialog(null,
									swriter.toString());
							e.printStackTrace();
						}

					}
				}).start();

			} else if (_showPrintDialog) {// 显示打印机设置窗口
				new Thread(new Runnable() {

					@Override
					public void run() {
						try {
							JasperPrintManager.printReport(jasperPrint, true);
						} catch (JRException e) {
							StringWriter swriter = new StringWriter();
							PrintWriter pwriter = new PrintWriter(swriter);
							e.printStackTrace(pwriter);
							JOptionPane.showMessageDialog(null,
									swriter.toString());
							e.printStackTrace();
						}

					}
				}).start();

			} else {
				if (printCount == 0)
					printCount = 1;

				for (int i = 0; i < printCount; i++) {
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								JasperPrintManager.printReport(jasperPrint,
										false);
							} catch (JRException e) {
								StringWriter swriter = new StringWriter();
								PrintWriter pwriter = new PrintWriter(swriter);
								e.printStackTrace(pwriter);
								JOptionPane.showMessageDialog(null,
										swriter.toString());
								e.printStackTrace();
							}

						}
					}).start();
				}
			}

		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.getMessage());
			e.printStackTrace();
		} finally {
			if (conn != null)
				conn.disconnect();
		}
	

	}



}
